<?php


namespace App\Sevrice;


use Elasticsearch\ClientBuilder;


class Es
{
    private $es;
    private static $client;

    /**
     * Es constructor.
     */
    private function __construct()
    {
        $this->es = ClientBuilder::create()->build();
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    /**
     * @return Es
     */
    static function one()
    {
        if (!(self::$client instanceof self)) {
            self::$client = new self();
        }
        return self::$client;
    }

    /**
     * @param $name
     * @return array|callable
     * 创建es索引
     */
    function create($name)
    {
        $params = [
            'index' => $name,
            'type' => '_doc',
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'shop_nick' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ]
                    ]
                ]
            ]
        ];
        $response = $this->es->index($params);
        return $response;
    }

    /**
     * @param $id
     * @param $data
     * @return array|callable
     * 同步将数据添加到es索引中
     */
    function addInfo($id, $data)
    {
        $params = [
            'index' => 'shop',
            'type' => '_doc',
            'id' => $id,
            'body' => $data
        ];
        $response = $this->es->index($params);
        return $response;
    }

    /**
     * @param $size
     * @param $form
     * @param $shopNick
     * @return mixed
     * es分页搜索
     */
    function searchInfo($size, $form, $shopNick)
    {
        $params = [
            'index' => 'shop',
            'type' => '_doc',
            'body' => [
                'query' => [
                    'match' => [
                        'shop_nick' => $shopNick
                    ]
                ],
                "highlight" => [
                    "pre_tags" => ["<span style='color:red'>"],
                    "post_tags" => ["</span>"],
                    "fields" => [
                        "shop_nick" => new \stdClass()
                    ]
                ],
                "size" => $size,
                "from" => $form
            ]
        ];
        $response = $this->es->search($params)['hits'];
        $data = [];
        $arr['last_page'] = ceil($response['total']['value'] / $size);
        foreach ($response['hits'] as $v) {
            $v['_source']['shop_nick'] = $v['highlight']['shop_nick'];
            array_push($data, $v['_source']);
        }
        $arr['data'] = $data;
        return $arr;
    }

}